/**
 * Get an APD list from the luo_rudy_1991 model
 * @author Romel
 */

public class APD{
	public static void main(String[] args) {
		// We launch the model
		luo_rudy_I_model_1991 model = new luo_rudy_I_model_1991();
		
		// We put the delta value
		double delta = 0.01;
		
		//  Variables definitions
		double t_start;
		double apd;
		double t;
		int ap;
		double previous = model.Y[4];
		
		// We initialise the file
		System.out.println("Period\tAPD");
		
		// We take values between 400 and 2000
		for(int period = 400; period <= 2000; period += 100){
			// Values initialisation
			t_start = 0.0;
			t 		= 0;
			ap 		= 0;
			model.init();
			model.membrane___stim_period = period;

			for(int j = 0; j < 300000; j++){
				// We compute the model with the actual time
				model.compute(t);
				
				// We calculate the next parameters value
				for (int i = 0; i <= 7; i++)
					model.Y[i] = model.Y[i] + delta * model.dY[i];

				if(previous < -75 && model.Y[4] > -75){
					// It's t_start
					t_start = t;	
				}
				
				if(previous > -75 && model.Y[4] < -75){
					// We take the second value and we stop the loop
					if (ap == 2){
						// t is t_end so we calculates the apd
						apd = t - t_start;
					    System.out.println(period + "\t" +apd);
					    break;
					}
					// We count the number of value
					ap++;
				}
				// We increase t
				t += delta;
				// We save the old model value
				previous = model.Y[4];
			}		
		}		
	}
}
